/**
 * Created by xxcao on 2017/6/5.
 */
var url = require('url');
var uuid = require('uuid');

module.exports = {
    removeFavicon: function (req, res) {
        var pathname = url.parse(req.url).pathname;
        if(pathname === '/favicon.ico'){
            return false;
        }
        return true;
    },
    toJson:function (code = 0, message = '', data) {
        var res = null;
        if (data) {
            res = { response : JSON.stringify({code, message, data}) };
        } else {
            res = { response : JSON.stringify({code, message}) };
        }
        var date = new Date();
        console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(),'RESPONSE:' + JSON.stringify(res)]);
        return res;
    },
    isParameterEmpty:function () {
        for(var i = 0; i < arguments.length; i++){
            if (arguments[i] === undefined ||
                arguments[i] === null ||
                arguments[i] === ''){
                return true;
            }
        }
        return false;
    },
    isValidSession:function (req, method) {
        if (method === 'GET' || method === 'get') {
            if (req.session.user === null || req.session.user !== req.query.user_id) {
                return false;
            }
        } else {
            if (req.session.user === null|| req.session.user !== req.body.user_id) {
                return false;
            }
        }
        return true;
    },
    pageSize:function () {
        return 10;
    },
    getUUID:function () {
        var uuidStr = uuid.v1();
        return uuidStr.replace(/-/g,'');
    },
    logInfo:function (type, name, method, data) {
        var date = new Date();
        if (type === 0){
            //request
            console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(), 'NAME:' + name, 'METHOD:' + method, 'REQUEST:' + JSON.stringify(data)]);
        } else {
            //response
            console.log(['TIME:' + date.toTimeString() + " " + date.toDateString(), 'NAME:' + name, 'METHOD:' + method, 'RESPONSE:' + JSON.stringify(data)]);
        }
    }
};
